\ Simple GUID library
\ Tom Dixon
\ 8/15/2008

\ *! GUID
\ *T Globally Unique IDentification (GUID) Library

\ *P This library consists of a few words that are for defining
\ ** GUIDs. GUIDs are used mostly by Foreign Function Interfaces
\ ** such as COM.  In essence, they are just 128-bit identifiers,
\ ** but usually are expressed in a format of
\ ** {00000000-0000-0000-0000-000000000000}.

\ Defining GUIDs

: hatoi 0 0 2swap >number 2drop drop ;

: parsescan- ( addr len -- addr len addr len )
   2dup [char] - scan 2dup 2>r nip - 2r> [char] - skip 2swap ;

: (Guid,) ( addr len -- ) \ comments in a guid
   Base @ >r HEX
   dup 38 <>
   err_GUIDlen ?throw
\   abort" Invalid Guid Length"
   1 /string
   parsescan- hatoi ,
   parsescan- hatoi w,
   parsescan- hatoi w,
   parsescan- drop 2 0 do dup i 2* + 2 hatoi c, loop drop
   parsescan- drop 6 0 do dup i 2* + 2 hatoi c, loop 2drop drop
   r> base ! ;

: Guid, ( -- )
\ *G Parses the input stream for a GUID and inserts it into the
\ ** dictionary.
   BL Word count (Guid,) ;

: CLSID>Str ( addr -- str len )
\ *G Converts a CLSID (or GUID) to a string
   pad swap fStringFromCLSID
   err_BadCLSID ?throw
\   abort" Not a CLSID!"
   pad @ 0
   begin over w@ while 2 2 d+ repeat nip pad @ swap ;

: UUID ( <name> "guid" -- )
\ *G Defines a UUID, e.g.
\ *C   Example: UUID test {01234567-89AB-CDEF-0000-000123456789}
   create guid, ;

